package com.homwee.listMenu;

import java.util.List;

import com.homwee.factory.FactoryActivity;
import com.homwee.factory.R;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.listMenu.util.MenuItemView;
import com.homwee.listMenu.util.MenuListView;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListMenu extends RelativeLayout{
	protected RelativeLayout mBgLayout;
	LinearLayout mBgLayout1;
	LinearLayout mBgLayout2;
	protected TextView mVersion;
	protected TextView mVersionDate;
	protected  TextView mTextItem;
	protected TextView mPageIndex;
	protected LinearLayout mPageItem;
	protected TextView mHelpInfo;
	protected MenuListView mListMenu;
	protected LayoutParams mParams;
	protected Drawable divider;
	protected Context mContext;
	protected FactoryActivity mFactory;
	protected OnTitleOnkeyListener mOnTitleOnkeyListener;
	private ImageView left_id;
	private ImageView right_id;

	public ListMenu(Context context) {
		super(context);
		mContext =context;
		if(mContext instanceof FactoryActivity){
			mFactory =(FactoryActivity) mContext;
		}
		mBgLayout =(RelativeLayout) LayoutInflater.from(context).inflate(R.layout.menu_list, null);
//		mParams =new LayoutParams(415,701);
//		addView(mBgLayout,mParams);
		addView(mBgLayout);
		mVersion =(TextView) mBgLayout.findViewById(R.id.version);
		mVersionDate =(TextView) mBgLayout.findViewById(R.id.versionDate);
		mPageIndex = (TextView)mBgLayout.findViewById(R.id.pageIndex);
		mPageItem =(LinearLayout) mBgLayout.findViewById(R.id.pageItem);
		mTextItem =(TextView) mBgLayout.findViewById(R.id.textItem);
		mHelpInfo =(TextView) mBgLayout.findViewById(R.id.helpInfo);
		mListMenu =(MenuListView) findViewById(R.id.itemListView);
		left_id = (ImageView) findViewById(R.id.left_id);
		right_id = (ImageView) findViewById(R.id.right_id);
		divider =mListMenu.getDivider();
		divider.setAlpha(0);
		mListMenu.setDivider(divider);
		mPageItem.setNextFocusDownId(mListMenu.getId());
		mPageItem.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if(arg1){
					mPageIndex.setTextColor(getResources().getColor(R.color.pink));
				}else{
					mPageIndex.setTextColor(getResources().getColor(R.color.white));
				}
			}
		});
		mPageItem.setOnKeyListener(new View.OnKeyListener(){
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				//Log.d("zcj", "onKey: "+keyCode);
				if(mOnTitleOnkeyListener!=null) {
					mOnTitleOnkeyListener.onKey(keyCode);
				}
				return false;
			}
		});
		// TODO Auto-generated constructor stub
	}
	public ListMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public ListMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	public void init(List<MenuItemData> mListItemData , int pageNum){
		init(mListItemData, pageNum, 0);
		mPageItem.requestFocus();
	}
	public void init(List<MenuItemData> mListItemData ,int pageNum ,int itemIndex){
		mListMenu.init(mListItemData ,itemIndex);
		if(mListItemData.get(0).mTotalPages != null){
			mPageIndex.setVisibility(VISIBLE);
			mPageItem.setBackground(getResources().getDrawable(R.drawable.menu_item_selector));
			mPageIndex.setText(pageNum + "/" +mListItemData.get(0).mTotalPages);
		}else {
			mPageIndex.setVisibility(GONE);
			mPageItem.setBackground(getResources().getDrawable(R.drawable.menu_item_selector2));
			left_id.setVisibility(GONE);
			right_id.setVisibility(GONE);

		}
		mTextItem.setText(mListItemData.get(0).mTitleName);
	}
	public MenuItemData getCurMenuItemData(){
		MenuItemData data =null;
		MenuItemView view =(MenuItemView) mListMenu.getSelectedView();
		if(view !=null){
			data =view.mMenuData;
		}
		return data;
	}
	public void setVersion(String version){
		mVersion.setTextColor(0xffffffff);
		mVersion.setText(version);
	}
	public void setVersionDate(String versionDate){
		mVersionDate.setTextColor(0xffffffff);
		mVersionDate.setText(versionDate);
	}
	public void setHelpInfo(String versionDate){
		mHelpInfo.setText(versionDate);
	}
	public void setHelpInfoColor(int color){
		mHelpInfo.setTextColor(color);
	}
	public void setHelpInfoVisible(boolean visible){
		if(visible)
			mHelpInfo.setVisibility(View.VISIBLE);
		else
			mHelpInfo.setVisibility(View.GONE);
	}
	public View getPageItem(){
		return mPageItem;
	}
	public void showMenu(boolean show){
		if(show){
			this.setVisibility(View.VISIBLE);
		}else{
			this.setVisibility(View.GONE);
		}
		
	}

	public void setOnTitleOnkeyListener(OnTitleOnkeyListener onTitleOnkeyListener){
		this.mOnTitleOnkeyListener=onTitleOnkeyListener;
	}

	public interface OnTitleOnkeyListener{
		void onKey(int keyCode);
	}
}